package com.ruoyi.fitsculpt.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.fitsculpt.domain.EasyfitExerciseLogs;
import com.ruoyi.fitsculpt.service.IEasyfitExerciseLogsService;
import com.ruoyi.fitsculpt.domain.EasyfitUsers;
import com.ruoyi.fitsculpt.service.IEasyfitUsersService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 运动打卡记录Controller
 *
 * @author metaee
 * @date 2025-02-27
 */
@Api(tags = "运动打卡记录管理")
@RestController
@RequestMapping("/fitsculpt/exercise/logs")
public class EasyfitExerciseLogsController extends BaseController {
    @Autowired
    private IEasyfitExerciseLogsService easyfitExerciseLogsService;

    @Autowired
    private IEasyfitUsersService easyfitUsersService;

    /**
     * 查询运动打卡记录列表
     */
    @ApiOperation("查询运动打卡记录列表")
    @GetMapping("/list")
    public TableDataInfo list(EasyfitExerciseLogs easyfitExerciseLogs) {
        startPage();
        List<EasyfitExerciseLogs> list = easyfitExerciseLogsService.selectEasyfitExerciseLogsList(easyfitExerciseLogs);
        for (EasyfitExerciseLogs log : list) {
            if (log.getUserId() != null) {
                EasyfitUsers user = easyfitUsersService.selectEasyfitUsersById(log.getUserId());
                log.setUser(user);
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出运动打卡记录列表
     */
    @ApiOperation("导出运动打卡记录列表")
    @Log(title = "运动打卡记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EasyfitExerciseLogs easyfitExerciseLogs) {
        List<EasyfitExerciseLogs> list = easyfitExerciseLogsService.selectEasyfitExerciseLogsList(easyfitExerciseLogs);
        ExcelUtil<EasyfitExerciseLogs> util = new ExcelUtil<EasyfitExerciseLogs>(EasyfitExerciseLogs.class);
        util.exportExcel(response, list, "运动打卡记录数据");
    }

    /**
     * 获取运动打卡记录详细信息
     */
    @ApiOperation("获取运动打卡记录详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        EasyfitExerciseLogs log = easyfitExerciseLogsService.selectEasyfitExerciseLogsById(id);
        if (log != null && log.getUserId() != null) {
            EasyfitUsers user = easyfitUsersService.selectEasyfitUsersById(log.getUserId());
            log.setUser(user);
        }
        return success(log);
    }

    /**
     * 新增运动打卡记录
     */
    @ApiOperation("新增运动打卡记录")
    @Log(title = "运动打卡记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EasyfitExerciseLogs easyfitExerciseLogs) {
        return toAjax(easyfitExerciseLogsService.insertEasyfitExerciseLogs(easyfitExerciseLogs));
    }

    /**
     * 修改运动打卡记录
     */
    @ApiOperation("修改运动打卡记录")
    @Log(title = "运动打卡记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EasyfitExerciseLogs easyfitExerciseLogs) {
        return toAjax(easyfitExerciseLogsService.updateEasyfitExerciseLogs(easyfitExerciseLogs));
    }

    /**
     * 删除运动打卡记录
     */
    @ApiOperation("删除运动打卡记录")
    @Log(title = "运动打卡记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{logIds}")
    public AjaxResult remove(@ApiParam(value = "运动打卡记录ID数组", required = true) @PathVariable Long[] logIds) {
        return toAjax(easyfitExerciseLogsService.deleteEasyfitExerciseLogsByLogIds(logIds));
    }
}
